

package com.bolt.convention.validation.validators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class CharValidatorUtil {

    /**
     * 根据Unicode编码完美的判断中文汉字
     * @param c
     * @return
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B) {
            return true;
        } else if (ub == Character.UnicodeBlock.PRIVATE_USE_AREA
                && c > '\uE000' && c < '\uF8FF') {
            return true;
        }
        return false;
    }

    /**
     * 完整的判断中文汉字
     * @param strName
     * @return
     */
    public static boolean isChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (!isChinese(c)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 根据Unicode编码完美的判断中文汉字和标点符号
     * @param c
     * @return
     */
	public static boolean isChineseOrPunctuation(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
			return true;
		} else if (ub == Character.UnicodeBlock.PRIVATE_USE_AREA
				&& c > '\uE000' && c < '\uF8FF') {
			return true;
		}
		return false;
	}

    /**
     * 完整的判断中文汉字和标点符号
     * @param strName
     * @return
     */
    public static boolean isChineseOrPunctuation(String strName) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (!isChineseOrPunctuation(c)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 是否含有标点符号
     * @param str
     * @return
     */
    public static boolean hasPunctuation(String str) {
        String regEx = "[`~!@#$%^&*()+=|{}':;',\\[\\]._<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(str);
        return m.find();
    }

    /**
     * 是否含有数字
     * @param str
     * @return
     */
    public static boolean hasNumber(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {
                return true;
            }
        }
        return false;
    }
}
